package laMalinche.AnnotatedRules;

import laMalinche.rete.rule.BusinessRuleType;
import laMalinche.rete.rule.Action;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.annotation.Annotation;

/**
 * Created by IntelliJ IDEA.
 * User: ranran
 * Date: Aug 14, 2009
 * Time: 7:01:48 AM
 * To change this template use File | Settings | File Templates.
 */
public class AnnotateMethod< T extends Class<? extends Annotation>> {
    Method method;
    InScopeOf<?> inScopeOf;
    static Class<? extends Annotation> annotaionCLASS = Rule.class;

    public AnnotateMethod(Method method, InScopeOf<?> inScopeOf) {
        this.method = method;
        this.inScopeOf = inScopeOf;
    }

    InScopeOf<?> getObject(){
        return inScopeOf;
    }

    public Method  getMethod(){
         return method;
    }

    public BusinessRuleType getBusinessRuleType(){
       return getannotation(method).value();
    }

      public static boolean isAnnotated(Method method){
       return getannotation(method) != null;
   }

    private static Rule getannotation(Method method) {
        return (Rule) method.getAnnotation(annotaionCLASS);
    }

    public Action getAction() {
        return new AnnotatedMethodToAction(this); 
    }

    public void invoke() {
        try {
            method.invoke(inScopeOf);
        } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvocationTargetException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
